/**
 * Copyright (C) 2009 Olafur Gauti Gudmundsson, Hjortur Stefan Olafsson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.gwt_control.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Olafur Gauti Gudmundsson
 */
public class ApplicationManager {

    private static final Map<String, ViewLinkHandler> viewLinkHandlers = new HashMap<String, ViewLinkHandler>();
    private static final List<ApplicationLifecycleListener> lifecycleListeners = new ArrayList<ApplicationLifecycleListener>();
    
    private static ApplicationController applicationController;
    private static ApplicationModel applicationModel;
    private static Controller startPageController;
    private static ApplicationEventListener applicationEventListener;
    private static ErrorHandler errorHandler = new DefaultErrorHandler();

    public static void registerView(ViewLinkHandler handler) {
        viewLinkHandlers.put(handler.getIdentifier(), handler);
    }

    public static ViewLinkHandler getView( String viewId ) {
        return viewLinkHandlers.get(viewId);
    }

    public static boolean isViewRegistered( String viewId ) {
        return viewLinkHandlers.containsKey(viewId);
    }

    public static void unregisterView( String viewId ) {
        viewLinkHandlers.remove(viewId);
    }

    public static void addLifecycleListener( ApplicationLifecycleListener listener ) {
        lifecycleListeners.add(listener);
    }

    public static List<ApplicationLifecycleListener> getLifecycleListeners() {
        return lifecycleListeners;
    }

    public static void setStartPageController( Controller startPageController ) {
        ApplicationManager.startPageController = startPageController;
    }

    public static Controller getStartPageController() {
        return startPageController;
    }

    public static ErrorHandler getErrorHandler() {
        return errorHandler;
    }

    public static void setErrorHandler(ErrorHandler errorHandler) {
        ApplicationManager.errorHandler = errorHandler;
    }

    public static ApplicationEventListener getApplicationEventListener() {
        return applicationEventListener;
    }

    public static void setApplicationEventListener( ApplicationEventListener applicationEventListener ) {
        ApplicationManager.applicationEventListener = applicationEventListener;
    }

    public static void setApplicationModel( ApplicationModel applicationModel ) {
        ApplicationManager.applicationModel = applicationModel;
    }

    public static void start() {
        start(new ApplicationController(applicationModel, applicationEventListener));
    }

    public static <T extends ApplicationController> void start( T appController ) {
        applicationController = appController;
        applicationController.init();
    }

    public static ApplicationController controller() {
        return applicationController;
    }

    public static ApplicationModel model() {
        return applicationController.getApplicationModel();
    }

    public static ApplicationView view() {
        return applicationController.getApplicationView();
    }
}
